//Bone info is in shared.lua
local PLAYER = FindMetaTable("Player");

function PLAYER:AddGear(item)

		local Gear = self:CharacterGetData("Gear");
		
		if Gear[item.WeaponObject] or !item.WearBone then return; end
		
		local BoneIndex = self:LookupBone( item.WearBone );
		local BonePos, BoneAng = self:GetBonePosition( BoneIndex );
		
		Gear[item.WeaponObject] = ents.Create("player_gear");--I should probably index things by it's wear bone.
	
			Gear[item.WeaponObject]:SetParent(self);
			Gear[item.WeaponObject]:SetPos( BonePos );
			Gear[item.WeaponObject]:SetAngles( BoneAng );

			Gear[item.WeaponObject]:SetDTInt( 1, BoneIndex );
			Gear[item.WeaponObject]:SetDTEntity( 1, self );
			Gear[item.WeaponObject]:SetDTInt( 2, item.WearType );
			Gear[item.WeaponObject]:SetDTBool( 1, false );

			Gear[item.WeaponObject]:SetModel(item.Model);
			
		Gear[item.WeaponObject]:Spawn();
		
		self:CharacterSetData( self:GetData("SelectedCharacter"), "Gear", Gear );
		
		return;
		
end

function PLAYER:RemoveAllGear()

	local Gear = self:CharacterGetData("Gear");

	for k, v in pairs( Gear ) do
			
		v:Remove();
		
	end
	
	Gear = {};
	self:CharacterSetData( self:GetData("SelectedCharacter"), "Gear", Gear );
	
end

function PLAYER:RemoveGear(ID)
	
	local Gear = self:CharacterGetData("Gear");

	for k, v in pairs( Gear ) do
	
		if k == ID then
			
			v:Remove();
			Gear[k] = nil;
			
		end
		
	end
		
	self:CharacterSetData( self:GetData("SelectedCharacter"), "Gear", Gear );

end

/*////////////////////////////////////////
			*Gear Utility*
*/////////////////////////////////////////

--Converts a Weapon String name to it's item data.
function GAME.WeaponToItem(weapon)

	for key, value in pairs(GAME.ItemList) do
		
		if value.WeaponObject == weapon then
			
			return GAME.ItemList[key];
			
		end
		
	end
	
end

hook.Add("PlayerDeath", "GAME.PlayerDeath", function(ply, weapon, inflictor)

	local Gear = ply:CharacterGetData("Gear");

	for k, v in pairs( Gear ) do
			
		GAME.SpawnItem( GAME.WeaponToItem(k), v:GetPos(), v:GetAngles() );
		
	end
	
	ply:RemoveAllGear();

end)


